﻿/*
	wp8libflac project
	© Alovchin, 2014
*/

using System;
using System.Threading;
using System.Threading.Tasks;
using Windows.Foundation.Collections;
using Windows.Media.Playback;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641

namespace wp81cs.App
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage
    {
        public MainPage()
        {
            this.InitializeComponent();

            NavigationCacheMode = NavigationCacheMode.Required;
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // TODO: Prepare page for display here.

            // TODO: If your application contains multiple pages, ensure that you are
            // handling the hardware Back button by registering for the
            // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
            // If you are using the NavigationHelper provided by some templates,
            // this event is handled for you.
        }

        private async void OnTestButtonClick(object sender, RoutedEventArgs e)
        {
            var autoResetEvent = new AutoResetEvent(false);
            BackgroundMediaPlayer.MessageReceivedFromBackground += (o1, e1) =>
            {
                if (e1.Data.ContainsKey("BackgroundPlayerStarted"))
                {
                    autoResetEvent.Set();
                }
            };
            
#error Add your test file to the 'Audio' folder of this project.

            var file = await StorageFile.GetFileFromApplicationUriAsync(
                new Uri("ms-appx:///Audio/yourfile.flac"));

            var isSuccess = await Task.Run(() => autoResetEvent.WaitOne(2000));
            if (isSuccess)
            {
                BackgroundMediaPlayer.SendMessageToBackground(new ValueSet {{"AddTrack", file.Path}});
            }
        }
    }
}
